package com.yanqu.road.server.manger.marriage.task;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.entity.marriage.MarriageRecord;
import com.yanqu.road.entity.player.UserMarriageData;
import com.yanqu.road.entity.task.args.union.UnionActivityTaskArgs;
import com.yanqu.road.logic.pb.MarriagePb;
import com.yanqu.road.pb.marriage.MarriageProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.gameplayer.module.player.MarriageModule;
import com.yanqu.road.server.gameplayer.module.xiaomian.XiaoMianModule;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.executor.GeneralTask;

import java.math.BigInteger;

public class PartnerMarrySuccessTask extends GeneralTask {
    private MarriageInfo myChild;
    private MarriageInfo partnerChild;
    private boolean isCrossMarriage;

    public PartnerMarrySuccessTask(MarriageInfo myChild, MarriageInfo partnerChild, boolean isCrossMarriage) {
        this.myChild = myChild;
        this.partnerChild = partnerChild;
        this.isCrossMarriage = isCrossMarriage;
    }

    @Override
    public void doRun() {
        GamePlayer player = GamePlayerMgr.getPlayer(myChild.getUserId());
        if(null != player){
            //发放道具奖励
            BigInteger addEarnSpeed = MarriageMgr.mailSendMarriageReward(myChild, partnerChild, player.getUserInfo(), isCrossMarriage);
            //生成记录
            MarriageRecord marriageRecord = MarriageMgr.addMarriageRecord(myChild, partnerChild, addEarnSpeed);
            MarriageModule marriageModule = player.getModule(MarriageModule.class);
            UserMarriageData marriageData = marriageModule.getUserMarriageData();
            if(null != marriageData) {
                marriageData.setTotalMarriageTimes(marriageData.getTotalMarriageTimes() + 1);
            }
            player.getModule(MarriageModule.class).syncUserMarriageData();
            MarriageProto.MarriageRecordTempMsg.Builder msg = MarriagePb.parseMarriageRecordTempMsg(marriageRecord);
            if(null != msg) {
                player.sendPacket(Protocol.U_MARRIAGE_SUCCESS_SYNC, msg);
            }
            //成就、任务
            player.notifyListener(eGamePlayerEventType.MarriageTimes.getValue(), 1);
            player.notifyListener(eGamePlayerEventType.UnionMarriageTimes.getValue(), new UnionActivityTaskArgs(UnionActivityTaskArgs.TYPE_ADD_VALUE, 1, player.getUserInfo().getUnionUid()));
            player.notifyListener(eGamePlayerEventType.TotalChildMarriageTime.getValue(),1);
            player.notifyListener(eGamePlayerEventType.MarriageTotalTimes.getValue(), marriageData);
            player.notifyListener(eGamePlayerEventType.ChildEarnSpeedRiseRank.getValue(), addEarnSpeed);
            //小面任务
            player.getModule(XiaoMianModule.class).addMarriageTimes(1);
            player.getModule(EarnSpeedModule.class).scheduleAddSilver();
        }
    }
}
